package com.itheima.service.impl;

import com.itheima.dto.MenuDTO;
import com.itheima.entity.SysMenu;
import com.itheima.mapper.MenuMapper;
import com.itheima.service.MenuService;
import com.itheima.vo.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单操作类
 *
 * @author 13225
 * @date 2023/09/20
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    private final MenuMapper menuMapper;

    public MenuServiceImpl(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    public List<MenuVO> allData() {
        List<SysMenu> allMen = menuMapper.getPid(0);
        List<MenuVO> allList = new ArrayList<>();
        allMen.forEach(meun -> {
            allList.add(meun.getMenv());
        });
        allList.forEach(menuVO -> {
            List<MenuVO> allList1 = new ArrayList<>();
            List<SysMenu> allMen1 = menuMapper.getPid(menuVO.getId());
            allMen1.forEach(m -> {
                allList1.add(m.getMenv());
            });
            allList1.forEach(v -> {
                List<MenuVO> allList2 = new ArrayList<>();
                List<SysMenu> allMen2 = menuMapper.getPid(v.getId());
                allMen2.forEach(e -> {
                    allList2.add(e.getMenv());
                });
                v.setChildren(allList2);
            });
            menuVO.setChildren(allList1);
        });
        return allList;
    }

    @Override
    public void addMenu(MenuDTO menuDTO) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setPid(menuDTO.getPid());
        sysMenu.setPath(menuDTO.getPath());
        sysMenu.setComponent(menuDTO.getComponent());
        sysMenu.setTitle(menuDTO.getTitle());
        sysMenu.setIcon(menuDTO.getIcon());
        sysMenu.setIsShow(menuDTO.getIs_show());
        sysMenu.setIsCache(menuDTO.getIs_cache());
        sysMenu.setIsLink(menuDTO.getIs_link());
        sysMenu.setRedirect(menuDTO.getRedirect());
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setType(menuDTO.getType());
        sysMenu.setSort(menuDTO.getSort());
        sysMenu.setMark(menuDTO.getMark());
        sysMenu.setDefaultData("1");
        menuMapper.addMenu(sysMenu);
    }

    @Override
    public void delMenu(String id) {
        menuMapper.delMenu(id);
    }

    @Override
    public void updateMenu(MenuDTO menuDTO) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(menuDTO.getId());
        sysMenu.setPid(menuDTO.getPid());
        sysMenu.setPath(menuDTO.getPath());
        sysMenu.setComponent(menuDTO.getComponent());
        sysMenu.setTitle(menuDTO.getTitle());
        sysMenu.setIcon(menuDTO.getIcon());
        sysMenu.setIsShow(menuDTO.getIs_show());
        sysMenu.setIsCache(menuDTO.getIs_cache());
        sysMenu.setIsLink(menuDTO.getIs_link());
        sysMenu.setRedirect(menuDTO.getRedirect());
        sysMenu.setUpdateDate(LocalDateTime.now());
        sysMenu.setType(menuDTO.getType());
        sysMenu.setSort(menuDTO.getSort());
        sysMenu.setMark(menuDTO.getMark());
        sysMenu.setDefaultData("1");
        menuMapper.updateMenu(sysMenu);
    }

    @Override
    public MenuVO rowInfoMenu(String id) {
        SysMenu sysMenu = menuMapper.getMenu(id);
        MenuVO menv = sysMenu.getMenv();
        return menv;
    }
}
